package com.soft.util.encrypt;

public class EncryptTea {
    private static String[] keyStringArray = new String[]{"3A", "DA", "75", "21", "DB", "E2", "DB", "B3", "11", "B4", "49", "01", "A5", "C6", "EA", "D4"};
    private static byte[] key = null;
    private static int[] k = null;
    private static final int times = 32;

    public EncryptTea() {
    }

    private static byte[] tea_encrypt(byte[] buf) {
        int cycle = buf.length / 4;
        int[] v = new int[cycle];
        byte[] temp = null;

        for(int j = 0; j < cycle; ++j) {
            temp = new byte[4];
            System.arraycopy(buf, j * 4, temp, 0, 4);
            v[j] = DataTypeSwitch.bytesToInt(temp);
            temp = null;
        }

        int y = v[0];
        int z = v[1];
        int sum = 0;
        int delta = -1640531527;
        int a = k[0];
        int b = k[1];
        int c = k[2];
        int d = k[3];

        for(int i = 0; i < 32; ++i) {
            sum += delta;
            y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
            z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
        }

        v[0] = y;
        v[1] = z;
        byte[] sumMi = new byte[8];
        byte[] mi1 = DataTypeSwitch.intToByte(v[0]);
        byte[] mi2 = DataTypeSwitch.intToByte(v[1]);
        System.arraycopy(mi1, 0, sumMi, 0, 4);
        System.arraycopy(mi2, 0, sumMi, 4, 4);
        return sumMi;
    }

    private static byte[] tea_decrypt(byte[] buf) {
        int cycle = buf.length / 4;
        int[] v = new int[cycle];
        byte[] temp = null;

        for(int j = 0; j < cycle; ++j) {
            temp = new byte[4];
            System.arraycopy(buf, j * 4, temp, 0, 4);
            v[j] = DataTypeSwitch.bytesToInt(temp);
            temp = null;
        }

        int y = v[0];
        int z = v[1];
        int sum = -957401312;
        int delta = -1640531527;
        int a = k[0];
        int b = k[1];
        int c = k[2];
        int d = k[3];

        for(int i = 0; i < 32; ++i) {
            z -= (y << 4) + c ^ y + sum ^ (y >> 5) + d;
            y -= (z << 4) + a ^ z + sum ^ (z >> 5) + b;
            sum -= delta;
        }

        v[0] = y;
        v[1] = z;
        byte[] sumMi = new byte[8];
        byte[] mi1 = DataTypeSwitch.intToByte(v[0]);
        byte[] mi2 = DataTypeSwitch.intToByte(v[1]);
        System.arraycopy(mi1, 0, sumMi, 0, 4);
        System.arraycopy(mi2, 0, sumMi, 4, 4);
        return sumMi;
    }

    public static void tea_encrypt_fun(byte[] buf, int len) {
        int cycle = len / 8;

        for(int i = 0; i < cycle; ++i) {
            byte[] temp = new byte[8];
            System.arraycopy(buf, i * 8, temp, 0, 8);
            byte[] decodeByte = tea_encrypt(temp);
            System.arraycopy(decodeByte, 0, buf, i * 8, 8);
        }

    }

    public static void tea_decrypt_fun(byte[] buf, int len) {
        int cycle = len / 8;

        for(int i = 0; i < cycle; ++i) {
            byte[] temp = new byte[8];
            System.arraycopy(buf, i * 8, temp, 0, 8);
            byte[] decodeByte = tea_decrypt(temp);
            System.arraycopy(decodeByte, 0, buf, i * 8, 8);
        }

    }

    static {
        key = new byte[16];

        for(int k = 0; k < key.length; ++k) {
            key[k] = (byte)Integer.parseInt(keyStringArray[k], 16);
        }

        byte[] key0 = new byte[4];
        System.arraycopy(key, 0, key0, 0, 4);
        byte[] key1 = new byte[4];
        System.arraycopy(key, 4, key1, 0, 4);
        byte[] key2 = new byte[4];
        System.arraycopy(key, 8, key2, 0, 4);
        byte[] key3 = new byte[4];
        System.arraycopy(key, 12, key3, 0, 4);
        EncryptTea.k = new int[4];
        EncryptTea.k[0] = DataTypeSwitch.bytesToInt(key0);
        EncryptTea.k[1] = DataTypeSwitch.bytesToInt(key1);
        EncryptTea.k[2] = DataTypeSwitch.bytesToInt(key2);
        EncryptTea.k[3] = DataTypeSwitch.bytesToInt(key3);
    }
}
